package com.d17_homework.homework6;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Set;

public class Demo6 {
    public static void main(String[] args) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", "heima001");
        map.put("name", "张三");
        map.put("age", 23);
        map.put("birthday", "1999-09-09");
        //获取class对象
        Class<?> clazz = Class.forName("com.d17_homework.homework6.Student6");
        //创建学生类对象
        Student6 stu = (Student6) clazz.getDeclaredConstructor().newInstance();
        //获取所有方法
        Method[] methods = clazz.getMethods();
        //遍历map集合，使得集合中键与方法匹配
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            Object value = map.get(key);
            for (Method method : methods) {
                if ("set".concat(key.substring(0, 1).toUpperCase().concat(key.substring(1))).equals(method.getName())) {
                    method.invoke(stu, value);
                }
            }
        }
        System.out.println(stu);
    }
}
